﻿using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using AutoMapper.QueryableExtensions;
using Newtonsoft.Json;
using Microsoft.AspNet.Identity;

namespace Hrm.Services
{
    public class ServiceService : BaseService<Service>, IServiceService
    {
        IServiceStudioDbRepository _serviceStudioDbRepository;
        IPriceTypeStudioDbRepository _priceTypeStudioDbRepository;
        IPriceItemStudioDbRepository _priceItemStudioDbRepository;

        public ServiceService(IServiceStudioDbRepository serviceStudioDbRepository, IPriceTypeStudioDbRepository priceTypeStudioDbRepository, IPriceItemStudioDbRepository priceItemStudioDbRepository)
            : base(serviceStudioDbRepository)
        {
            this._serviceStudioDbRepository = serviceStudioDbRepository;
            this._priceTypeStudioDbRepository = priceTypeStudioDbRepository;
            this._priceItemStudioDbRepository = priceItemStudioDbRepository;
        }

        public bool AddPriceType(PriceType entity)
        {
            if (this.CurrentUser != null)
            {
                entity.CreateDatetime = DateTime.Now;
                entity.CreateUserCode = this.CurrentUser.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = this.CurrentUser.Identity.Name + "-添加-" + typeof(PriceType).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, this.JsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = this.CurrentUser.Identity.GetUserId()
                });
            }
            return this._priceTypeStudioDbRepository.Insert(entity) > 0;
        }

        public bool UpdatePriceType(PriceType entity)
        {
            if (this.CurrentUser != null)
            {
                entity.UpdateDatetime = DateTime.Now;
                entity.UpdateUserCode = this.CurrentUser.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = this.CurrentUser.Identity.Name + "-更新-" + typeof(PriceType).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, this.JsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = this.CurrentUser.Identity.GetUserId()
                });
            }
            return this._priceTypeStudioDbRepository.Update(entity) > 0;
        }

        public List<PriceType> GetAllPriceType()
        {
            return this._priceTypeStudioDbRepository.GetList(p => p.IsEnable = true).OrderBy(p => p.CreateDatetime).ToList();
        }

        public bool AddPriceItem(PriceItem entity)
        {
            if (this.CurrentUser != null)
            {
                entity.CreateDatetime = DateTime.Now;
                entity.CreateUserCode = this.CurrentUser.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = this.CurrentUser.Identity.Name + "-添加-" + typeof(PriceItem).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, this.JsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = this.CurrentUser.Identity.GetUserId()
                });
            }
            return this._priceItemStudioDbRepository.Insert(entity) > 0;
        }

        public bool UpdatePriceItem(PriceItem entity)
        {
            if (this.CurrentUser != null)
            {
                entity.UpdateDatetime = DateTime.Now;
                entity.UpdateUserCode = this.CurrentUser.Identity.GetUserId();
                GlobalQueue.LogQueue.Enqueue(new Log()
                {
                    Origin = this.CurrentUser.Identity.Name + "-更新-" + typeof(PriceItem).Name,
                    Level = (int)EnumLogLevel.Trace,
                    Message = JsonConvert.SerializeObject(entity, this.JsonSetting),
                    StackTrade = string.Empty,
                    CreateDatetime = DateTime.Now,
                    CreateUserCode = this.CurrentUser.Identity.GetUserId()
                });
            }
            return this._priceItemStudioDbRepository.Update(entity) > 0;
        }

        public List<PriceItem> GetAllPriceItems()
        {
            return this._priceItemStudioDbRepository.GetAll().OrderBy(p => p.CreateDatetime).ToList();
        }

        public List<Service> TopServices(int topNum)
        {
            return this._serviceStudioDbRepository.GetList(p => p.IsEnable).OrderByDescending(p => p.CreateDatetime).Take(topNum).ToList();
        }

        public PageResponse<ServiceDto> GetPageList(PageRequest<ServiceDto> request)
        {
            var query = this.GetAllAbledAndOrderByCreatetime().AsQueryable();
            if (!string.IsNullOrEmpty(request.filter.Name))
            {
                query = query.Where(p => p.Name.Contains(request.filter.Name));
            }
            var data = query.Project().To<ServiceDto>();
            PageResponse<ServiceDto> result = new PageResponse<ServiceDto>(data, request.draw, request.start, request.length);
            return result;
        }


        public bool DeletePriceType(int id)
        {
            GlobalQueue.LogQueue.Enqueue(new Log()
            {
                Origin = this.CurrentUser.Identity.Name + "-删除-" + typeof(PriceType).Name,
                Level = (int)EnumLogLevel.Trace,
                Message = JsonConvert.SerializeObject(id, this.JsonSetting),
                StackTrade = string.Empty,
                CreateDatetime = DateTime.Now,
                CreateUserCode = this.CurrentUser.Identity.GetUserId()
            });
            var result = this._priceTypeStudioDbRepository.GetById(id);
            this._priceItemStudioDbRepository.Delete(result.PriceItems, false);
            return this._priceTypeStudioDbRepository.Delete(result) > 0;
        }

        public bool DeletePriceItem(int id)
        {
            GlobalQueue.LogQueue.Enqueue(new Log()
            {
                Origin = this.CurrentUser.Identity.Name + "-删除-" + typeof(PriceItem).Name,
                Level = (int)EnumLogLevel.Trace,
                Message = JsonConvert.SerializeObject(id, this.JsonSetting),
                StackTrade = string.Empty,
                CreateDatetime = DateTime.Now,
                CreateUserCode = this.CurrentUser.Identity.GetUserId()
            });
            return this._priceItemStudioDbRepository.Delete(id) > 0;
        }


        public PriceType GetPriceType(int id)
        {
            return this._priceTypeStudioDbRepository.GetById(id);
        }
    }
}
